import { createStore, applyMiddleware } from 'redux'
import reduxLogger from 'redux-logger' // redux日志中间件
import reduxThunk from 'redux-thunk' // 异步派发中间件
import reduxPromise from 'redux-promise' // 异步派发中间件
// import { createStore } from '../components/ReducComp/MyRedux'
const initState = {
	supNum: 0,
	oppNum: 0,
	supPercent: '0',
	supTian: 0,
	oppTian: 0,
	supTianPercent: '0'
}
const reducer = (state = initState, action) => {
	// state: 容器中的状态
	// action: 每一次基于dispatch发送的指令[要求：必须是一个对象，对象中必须有type属性]

	//为了接下来的操作，不会直接修改容器中的状态，而是等到最后return的时候，再修改，我们先克隆一份
	state = { ...state }
	switch (action.type) {
		case 'SUP':
			state.supNum++
			break
		case 'OPP':
			state.oppNum++
			break
		case 'SUP_TIAN':
			state.supTian++
			break
		case 'OPP_TIAN':
			state.oppTian++
			break
	}
	state.supPercent = ((state.supNum / (state.supNum + state.oppNum)) * 100).toFixed(2)
	state.supTianPercent = ((state.supTian / (state.supTian + state.oppTian)) * 100).toFixed(2)

	return state
}

/**
 * 创建Store容器
 */
const store = createStore(
	reducer,
	// 使用中间件
	applyMiddleware(reduxThunk, reduxPromise, reduxLogger)
)
export default store